# -*- coding:utf-8 -*-
import sys

from common.import_pyside import *
from .app_ui import App_UI

class MainWin(QMainWindow):

    def __init__(self):
        super(MainWin, self).__init__(parent=None)
        self.setWindowTitle("ToTest")
        self.setWindowIcon(QIcon(':/picture/icons/system.ico'))

        self.app_ui = App_UI(self)

        main_layout = QHBoxLayout()
        main_splitter = QSplitter(Qt.Orientation.Horizontal)

        main_layout.addWidget(self.app_ui)
        main_layout.addWidget(main_splitter)
        self.setLayout(main_layout)
        self.statusBar()

        # 关闭所有窗口时，不关闭系统
        QApplication.setQuitOnLastWindowClosed(False)

        # 系统图标
        self.tray = QSystemTrayIcon()
        self.tray.setIcon(QIcon(':/picture/icons/favicon.ico'))
        self.tray.setVisible(True)

        # 单击图标事件处理
        self.tray.activated.connect(self.SystemTrayIconClick)

        # 添加菜单项
        self._hide_action = QAction("隐藏窗口", self)
        self._hide_action.setIcon(QIcon(':/picture/icons/hide.ico'))
        # self._hide_action.triggered.connect(self._hide_action_click)

        self._show_action = QAction("显示窗口", self)
        self._show_action.setIcon(QIcon(':/picture/icons/show.ico'))
        self._show_action.setVisible(False)
        # self._show_action.triggered.connect(self._show_action_click)

        self._start_record_action = QAction("开始录制", self)
        self._start_record_action.setIcon(QIcon(':/picture/icons/start_record.ico'))
        # self._start_record_action.triggered.connect(self._start_record_action_click)

        self._pause_action = QAction("暂停录制", self)
        self._pause_action.setIcon(QIcon(':/picture/icons/recording.ico'))
        self._pause_action.setVisible(False)
        # self._pause_action.triggered.connect(self._pause_action_click)

        self._continue_record_action = QAction("继续录制", self)
        self._continue_record_action.setIcon(QIcon(':/picture/icons/start_record.ico'))
        self._continue_record_action.setVisible(False)
        # self._continue_record_action.triggered.connect(self._continue_record_action_click)

        self._stop_record_action = QAction("停止录制", self)
        self._stop_record_action.setIcon(QIcon(':/picture/icons/stop_record.ico'))
        self._stop_record_action.setVisible(False)
        # self._stop_record_action.triggered.connect(self._stop_record_action_click)

        self._config_action = QAction("配置设置", self)
        self._config_action.setIcon(QIcon(':/picture/icons/setting.ico'))
        self._config_action.setVisible(True)
        # self._config_action.triggered.connect(self._config_action_click)

        self._remote_action = QAction("开启远程连接", self)
        self._remote_action.setIcon(QIcon(':/picture/icons/start_record.ico'))
        self._remote_action.setVisible(True)
        # self._remote_action.triggered.connect(self._config_action_click)

        self._stop_remote_action = QAction("停止远程连接", self)
        self._stop_remote_action.setIcon(QIcon(':/picture/icons/stop_record.ico'))
        self._stop_remote_action.setVisible(True)
        # self._stop_remote_action.triggered.connect(self._config_action_click)

        self._quit_action = QAction("退出", self)
        self._quit_action.setIcon(QIcon(':/picture/icons/shutdown.ico'))
        app_instance = QApplication.instance()
        self._quit_action.triggered.connect(app_instance.quit)

        self._tray_icon_menu = QMenu()
        self._tray_icon_menu.addAction(self._hide_action)
        self._tray_icon_menu.addAction(self._show_action)
        self._tray_icon_menu.addSeparator()
        self._tray_icon_menu.addAction(self._start_record_action)
        self._tray_icon_menu.addAction(self._pause_action)
        self._tray_icon_menu.addAction(self._continue_record_action)
        self._tray_icon_menu.addAction(self._stop_record_action)
        self._tray_icon_menu.addSeparator()
        self._tray_icon_menu.addAction(self._config_action)
        self._tray_icon_menu.addSeparator()
        self._tray_icon_menu.addAction(self._remote_action)
        self._tray_icon_menu.addAction(self._stop_remote_action)
        self._tray_icon_menu.addSeparator()
        self._tray_icon_menu.addAction(self._quit_action)

        self.tray.setContextMenu(self._tray_icon_menu)

    def SystemTrayIconClick(self, *args, **kwargs):

        action = str(*args).split('.')[-1]
        print("TrayIconClick:" + action)

        if action == 'DoubleClick':
            # 双击，Mac没有DoubleClick
            # self._show_action_click()
            pass

        if action == 'Trigger':
            # 按下左键
            pass

        if action == 'Context':
            # 按下右键
            pass

        if action == 'MiddleClick':
            # 按下中键
            pass


def run_main_window():
    app = QApplication()
    app.setWindowIcon(QIcon(":/picture/icons/system.ico"))
    app.setStyle("Material")
    win = MainWin()
    win.show()
    sys.exit(app.exec())
